﻿using System;
using System.Collections.Generic;
using System.Linq;
/*
class Program
{
    static void Main(string[] args)
    {
        OrderService orderService = new OrderService();
        
        try
        {
            Order order1 = new Order(1, "Customer1")
            {
                Details = new List<OrderDetail>
                {
                    new OrderDetail("Product1", 2, 100),
                    new OrderDetail("Product2", 1, 200)
                }
            };
            orderService.AddOrder(order1);

            Order order2 = new Order(2, "Customer2")
            {
                Details = new List<OrderDetail>
                {
                    new OrderDetail("Product3", 3, 150)
                }
            };
            orderService.AddOrder(order2);
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
        
        var orders = orderService.QueryOrders(customer: "Customer1", productName: "Product1");
        foreach (var order in orders)
        {
            Console.WriteLine(order);
            foreach (var detail in order.Details)
            {
                Console.WriteLine(detail);
            }
        }
        
        try
        {
            orderService.DeleteOrder(1);
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
        
        try
        {
            orderService.UpdateOrder(2, "Customer3");
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
        
        orderService.SortOrders(o => o.GetTotalAmount());
        foreach (var order in orderService.orders)
        {
            Console.WriteLine(order);
        }
        
        orderService.SortOrdersByOrderId();
        foreach (var order in orderService.orders)
        {
            Console.WriteLine(order);
        }
    }
}
public class Order
{
    public int OrderId { get; set; }
    public string Customer { get; set; }
    public List<OrderDetail> Details { get; set; }

    public Order(int orderId, string customer)
    {
        OrderId = orderId;
        Customer = customer;
        Details = new List<OrderDetail>();
    }

    public override bool Equals(object obj)
    {
        if (obj is Order other)
        {
            return OrderId == other.OrderId;
        }
        return false;
    }

    public override int GetHashCode()
    {
        return OrderId.GetHashCode();
    }

    public override string ToString()
    {
        return $"Order ID: {OrderId}, Customer: {Customer}, Total Amount: {GetTotalAmount()}";
    }

    public decimal GetTotalAmount()
    {
        return Details.Sum(detail => detail.Amount);
    }
}

public class OrderDetail
{
    public string ProductName { get; set; }
    public int Quantity { get; set; }
    public decimal Price { get; set; }

    public OrderDetail(string productName, int quantity, decimal price)
    {
        ProductName = productName;
        Quantity = quantity;
        Price = price;
    }

    public decimal Amount => Quantity * Price;

    public override bool Equals(object obj)
    {
        if (obj is OrderDetail other)
        {
            return ProductName == other.ProductName && Quantity == other.Quantity && Price == other.Price;
        }
        return false;
    }

    public override int GetHashCode()
    {
        return ProductName.GetHashCode() ^ Quantity.GetHashCode() ^ Price.GetHashCode();
    }

    public override string ToString()
    {
        return $"Product: {ProductName}, Quantity: {Quantity}, Price: {Price}, Amount: {Amount}";
    }
}

public class OrderService
{
    public List<Order> orders = new List<Order>();

    public void AddOrder(Order order)
    {
        if (orders.Contains(order))
        {
            throw new InvalidOperationException("Order already exists.");
        }
        orders.Add(order);
    }

    public void DeleteOrder(int orderId)
    {
        var order = orders.FirstOrDefault(o => o.OrderId == orderId);
        if (order == null)
        {
            throw new InvalidOperationException("Order not found.");
        }
        orders.Remove(order);
    }

    public void UpdateOrder(int orderId, string customer)
    {
        var order = orders.FirstOrDefault(o => o.OrderId == orderId);
        if (order == null)
        {
            throw new InvalidOperationException("Order not found.");
        }
        order.Customer = customer;
    }

    public IEnumerable<Order> QueryOrders(string customer = null, string productName = null)
    {
        var query = orders.AsQueryable();

        if (customer != null)
        {
            query = query.Where(o => o.Customer == customer);
        }

        if (productName != null)
        {
            query = query.Where(o => o.Details.Any(d => d.ProductName == productName));
        }

        return query.OrderBy(o => o.GetTotalAmount()).ToList();
    }

    public void SortOrders(Func<Order, object> keySelector)
    {
        orders = orders.OrderBy(keySelector).ToList();
    }

    public void SortOrdersByOrderId()
    {
        orders = orders.OrderBy(o => o.OrderId).ToList();
    }
}


*/